/*
 *  Copyright 2002 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  
 */
/*
 *  ======== thrControl.c ========
 *  This file shows an example of sending messages from one thread to the
 *  other using mailbox.
 */
#include <std.h>

// DSP/BIOS includes
#include <mbx.h>
#include <tsk.h>

// RF includes
#include <utl.h>

// application includes
#include "appResources.h"   // application-wide common info
#include "appThreads.h"     // thread-wide common info
#include "thrControl.h"

#include "process/thrProcess.h"     /* contains thread specific information */

// global variables to be modified by GEL
#define NUMCONTROLS 4

//Structure for external control variables
typedef struct ExternalControl {
    Int setReference;	//0 or 1, to set reference frame
    Int yValue;			//New color values.  All three,
    Int crValue;		//Y, Cr, and Cb are passed together
    Int cbValue;		//in one message
} ExternalControl;

ExternalControl externalControl;        // GEL script writes here
ExternalControl externalControlPrev;    // this is a local copy 

/*
 *  ======== thrControlInit ========
 *
 */
Void thrControlInit()
{
    UTL_assert( NUMCONTROLS == 4 );

    // set the default Reference Frame and Colors
    externalControl.setReference = 1; 	//Set first frame as reference
    externalControl.yValue = 0;			//Initial color is red
    externalControl.crValue = 39;		//(y,cr,cb) = (0,39,255)
    externalControl.cbValue = 255;
    
    externalControlPrev = externalControl;
}

/*
 *  ======== thrControlStartup ========
 *
 */
Void thrControlStartup()
{    
    CtrlMsg txMsg1;
    CtrlMsg txMsg2;
    
    //Set initial reference frame to be 1st frame.
    //Only need to send message that we want a new
    //reference frame.
    txMsg2.cmd = MSGNEWREFERENCE;
    txMsg2.arg1 = 0;
    txMsg2.arg2 = 0;
    
    MBX_post(&mbxProcess, &txMsg2, 0);
    
    //Post Message for Color Value.  We encode the (Cr,Cb) 
    //pair into one value, so that we do not have to
    //post 3 messages.
    txMsg1.cmd = MSGNEWCOLOR;
    txMsg1.arg1 = externalControl.yValue;
    txMsg1.arg2 = (externalControl.crValue << 8) + externalControl.cbValue;
    
    MBX_post(&mbxProcess, &txMsg1, 0);
}

/*
 *  ======== thrControlRun ========
 *
 *  Main function of Control task.
 */
Void thrControlRun()
{
    CtrlMsg txMsg1;
    CtrlMsg txMsg2;
    
	// Main loop
	while (TRUE) {
	    
	    //See if user requested new reference frame
	    if (externalControl.setReference)
	    {
	    	txMsg1.cmd = MSGNEWREFERENCE;
	    	txMsg1.arg1 = 0;
	    	txMsg1.arg2 = 0;
	    	MBX_post(&mbxProcess, &txMsg1, 0);
	    	
	    	//Reset setReference to FALSE
	    	externalControl.setReference = 0;
	    }
	    
	    //See if user requested new color
	    //We encode (Cr,Cb) pair into one value so that
	    //we do not have to post 3 messages
	    if ((externalControl.yValue != externalControlPrev.yValue) ||
	    	(externalControl.crValue != externalControlPrev.crValue) ||
	    	(externalControl.cbValue != externalControlPrev.cbValue))
	    {
	    	txMsg2.cmd = MSGNEWCOLOR;
	    	txMsg2.arg1 = externalControl.yValue;
	    	txMsg2.arg2 = (externalControl.crValue << 8) + externalControl.cbValue;
	    	MBX_post(&mbxProcess, &txMsg2, 0);
	    }
	    
	    externalControlPrev = externalControl;
	    
	    // suspend self for 100 ticks, and then poll again
	    TSK_sleep( 100 );
	}
}


